package com.tgy.基础学习.Thread.Normal;

/**
 * @author 送你一块二向箔
 * @date 2021/11/08 14:03
 */
public class ThreadState {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                    System.out.println("大家好");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        });

        //观察状态
        Thread.State state = thread.getState();
        System.out.println(state);//NEW

        //观察启动后
        thread.start();
        state = thread.getState();
        System.out.println(state);//RUNNABLE

        while (state != Thread.State.TERMINATED){//只要线程不终止，一直输出状态
            thread.sleep(800);
            state = thread.getState();//更新值
            System.out.println(state);
            /*
            TIMED_WAITING
            TERMINATED
             */

        }
    }
}
